﻿using System;
using System.Linq;
using System.Xml.Linq;

namespace BookingEngine.Data
{
    public class HotelNewResvResponse
    {
        #region 基本属性

        private readonly XDocument _document;

        /// <summary>
        /// </summary>
        public bool IsSuccess
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var resultAttribute = _document.Root.Attribute("result");
                    if (resultAttribute != null)
                    {
                        return resultAttribute.Value.Equals("success", StringComparison.InvariantCultureIgnoreCase);
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// </summary>
        public string ErrorMessage
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    return _document.Root.Value;
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public DateTime CheckInDate
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("arrival").FirstOrDefault();
                    if (element != null)
                    {
                        return DateTime.Parse(element.Value);
                    }
                }
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// </summary>
        public DateTime CheckOutDate
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("departure").FirstOrDefault();
                    if (element != null)
                    {
                        return DateTime.Parse(element.Value);
                    }
                }
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// </summary>
        public DateTime BookDate
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("bookdate").FirstOrDefault();
                    if (element != null)
                    {
                        return DateTime.Parse(element.Value);
                    }
                }
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// </summary>
        public string Confnum
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("confnum").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string FirstName
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("firstname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string LastName
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("lastname").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// </summary>
        public string TotalPrice
        {
            get
            {
                if (_document != null && _document.Root != null)
                {
                    var element = _document.Root.Descendants("totalrevenue").FirstOrDefault();
                    if (element != null)
                    {
                        return element.Value;
                    }
                }
                return string.Empty;
            }
        }

        #endregion

        #region 构造函数

        /// <summary>
        /// </summary>
        /// <param name = "responseXml"></param>
        public HotelNewResvResponse(string responseXml)
        {
            _document = XDocument.Parse(responseXml);
        }

        #endregion
    }
}